#ifndef __HISTOGRAM_HPP__
#define __HISTOGRAM_HPP__

#include <vector>
#include <mutex>

#include "metrics/MetricType.hpp"

namespace metrics
{
    class ClientMetric;

    //累积频数直方图, 某个bucket的频数是小于等于此bucket的频数之和
    class Histogram
    {
    public:
        static const MetricType METRIC_TYPE{MetricType::Histogram};
        using BucketBoundaries = std::vector<double>;

    private:
        const BucketBoundaries _bucket_boundaries;
        mutable std::mutex _mutex;
        std::vector<double> _bucket_counts;
        double _sum;

    public:
        //根据bucket边界创建直方图
        Histogram(const BucketBoundaries &buckets);
        ~Histogram();

        /**
         * @brief 将value对应的bucket加1, 直方图sum增加value
         * 
         * @param value 属于某个bucket的值
         */
        void observe(double value);

        /**
         * @brief 直方图每个bucket都增加和bucket_increments对应的值, 并增加直方图总数, bucket_increments大小必须和BucketBoundaries一致
         * 
         * @param bucket_increments 每个bucket增加的数量
         * @param sum_of_values 直方图增加的总数, 直方图sum增加sum_of_values, sum_of_values表示抽样值之和
         */
        void observe(const std::vector<double> &bucket_increments, const double sum_of_values);

        ClientMetric collect() const;
    };

} // namespace metrics

#endif // __HISTOGRAM_HPP__